/**
 * Created by xuweijie on 17-5-18.
 */
/*
* 删除
* */
$(function () {
    $('.deladdress').click(function () {
        var addrid=$(this).attr('addrid');
        if(confirm("删除后将不可回复，是否需要删除")){
            var params = $.param({
                addrid: addrid
            }, true);
            $.ajax({
                url: '/cusdeladdr.action',
                type: 'post',
                data: params,
                dateType: 'json',
                success:function (data){
                    if(data=="0"){
                        window.location.href = "/front/address/showaddress";
                    }else {
                        alert("删除失败");
                    }
                }
            });
        }
    })
});


$(function () {
    //编辑
    $('.updateaddress').click(function () {
        var addrid=$(this).attr('addrid');
        var flag=$(this).attr('flag');
        var grandfather=$(this).parents('.panel-heading');
        var grandbrother=grandfather.next();
        var childs=$(grandbrother[0]).find('.panel-body');
        var fconsignee=$(childs[0]).find('font.fconsignee');
        var faddress=$(childs[0]).find('font.faddress');
        var fphone=$(childs[0]).find('font.fphone');
        var fpostcode=$(childs[0]).find('font.fpostcode');
        $('#upconsignee').val(fconsignee[0].innerText);
        $('#upaddress').val(faddress[0].innerText);
        $('#upphone').val(fphone[0].innerText);
        $('#uppostcode').val(fpostcode[0].innerText);
        $('.addrid').val(addrid);
        $('.flag').val(flag);
    });

    //添加新地址
    $('.insertaddress').click(function () {
        var flag=$(this).attr('flag');
        $('#upconsignee').val("");
        $('#upaddress').val("");
        $('#upphone').val("");
        $('#uppostcode').val("");
        $('.flag').val(flag);
    });

    // 模态框提交
    $('.upmodel').click(function () {
        var consignee=$('#upconsignee').val();
        var address=$('#upaddress').val();
        var phone=$('#upphone').val();
        var postcode=$('#uppostcode').val();
        var addrid=$('.addrid').val();
        var flag=$('.flag').val();
        var country=$('.country option:selected').val();
        var provience=$('.provience option:selected').val();
        var city=$('.city option:selected').val();
        if(consignee!=="" && address!=="" && phone!=="" && postcode!==""){
            var params = $.param({
                addrid:addrid,
                consignee: consignee,
                address: address,
                phone: phone,
                postcode: postcode,
                country: country,
                provience: provience,
                city: city
            }, true);
            var urlaction;
            if(flag==="1"){
                urlaction='/updateaddr.action';
            }else{
                urlaction='/insertaddr.action';
            }
            $.ajax({
                url: urlaction,
                type: 'post',
                data: params,
                dateType: 'json',
                success:function (data) {
                    if(data=="0"){
                        window.location.href = "/front/address/showaddress";
                    }else if(flag=="1" && data=="1"){
                        alert("地址更新失败");
                    }else if(flag=="0" && data=="1"){
                        alert("新地址添加失败");
                    }
                }
            });
            $('#updateModal').modal('hide');
        }else {
            alert("必填内容不允许空值");
        }
    });
});



